package com.sflx.shortmessage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sflx.shortmessage.entity.ShortUrl;
import com.sflx.shortmessage.service.ShortUrlService;
import com.sflx.shortmessage.util.NumericConvertUtils;
import com.sflx.shortmessage.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
import java.util.UUID;

/**
 * @author zengqingfa
 * @className ShortUrlController
 * @description
 * @create 2022/9/25 16:42
 */
@RestController
@Slf4j
public class ShortUrlController {

    @Resource
    private ShortUrlService shortUrlService;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 根据ID获取
     *
     * @param id
     * @return
     */
    @GetMapping("/shortUrl/getById")
    public ShortUrl getById(@RequestParam("id") Long id) {
        ShortUrl shortUrl = shortUrlService.getById(id);
        return shortUrl;
    }


    /**
     * 模拟的长链接地址
     *
     * @param shortUrl
     * @return
     */
    @PostMapping("/shortUrl/list")
    public List<ShortUrl> list(@RequestBody ShortUrl shortUrl) {
        List<ShortUrl> list = shortUrlService.list(new LambdaQueryWrapper<ShortUrl>()
                .eq(ShortUrl::getId, shortUrl.getId()));
        return list;
    }

    private static final String url = "localhost:8081/a/";

    @PostMapping("/shortUrl/save")
    public String save() {
        ShortUrl shortUrl = new ShortUrl();
        //生成对应的主键
        long id = snowFlake.nextId();
        //转换为对应的短链接地址 10进制转换为62进制
        String code = NumericConvertUtils.toOtherNumberSystem(id, 62);
        shortUrl.setId(id);
        shortUrl.setShortUrlCode(code);
        String longUrl = generateLongUrl();
        shortUrl.setLongUrl(longUrl);
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("md5");
            byte[] digest = md5.digest(longUrl.getBytes("utf-8"));
            //16是表示转换为16进制数
            String md5Str = new BigInteger(1, digest).toString(16);
            shortUrl.setLongUrlMd5(md5Str);
        } catch (Exception e) {
            log.error("md5加密异常：{}", e.getMessage());
        }
        shortUrlService.save(shortUrl);
        //发送短信:包含短链接
        String linkUrl = url + code;
        log.info("短信短链接：{}", linkUrl);
        return linkUrl;
    }


    /**
     * http://localhost:8081/shortUrl/list?id=1&shortUrlCode=1&longUrlMd5=xxxx&remark=111322&createUserCode=xxx
     *
     * @return
     */

    private static final String listUrl = "http://localhost:8081/shortUrl/list?id=1&";

    /**
     * 随机生成对应的长链接地址
     *
     * @return
     */
    public String generateLongUrl() {
        StringBuilder builder = new StringBuilder(listUrl);
        String uuid = UUID.randomUUID().toString().substring(16);
        builder.append("&shortUrlCode=" + uuid);
        builder.append("&longUrlMd5=" + uuid);
        builder.append("&remark=" + uuid);
        builder.append("&createUserCode=" + uuid);
        return builder.toString();
    }

}